package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.estatewar.UserEstateWarData;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserEstateWarDaoImpl extends TempDataObjectDao<UserEstateWarData> {

    public Map<Long, UserEstateWarData> getUserEstateWarDataMap() {
        Map<Long, UserEstateWarData> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_u_estate_war_info";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserEstateWarData data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public UserEstateWarData getUserEstateWarData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_u_estate_war_info where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserEstateWarData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserEstateWarData data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_estate_war_info(userId,type,pos,crossType,crossPos,maxType,maxPos,getDailyRewardTime,totalRobTimes,totalBeRobTimes,"
                + "totalDailyRewardCount,worshipValue,modifyTime,lastBeAttackNotifyTime,estateSkinId) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getType());
            call.setInt(i++, data.getPos());
            call.setInt(i++, data.getCrossType());
            call.setInt(i++, data.getCrossPos());
            call.setInt(i++, data.getMaxType());
            call.setInt(i++, data.getMaxPos());
            call.setLong(i++, data.getGetDailyRewardTime());
            call.setLong(i++, data.getTotalRobTimes());
            call.setLong(i++, data.getTotalBeRobTimes());
            call.setString(i++, data.getTotalDailyRewardCount().toString());
            call.setInt(i++, data.getWorshipValue());
            call.setLong(i++, data.getModifyTime());
            call.setLong(i++, data.getLastBeAttackNotifyTime());
            call.setInt(i++, data.getEstateSkinId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserEstateWarData data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_estate_war_info SET type=?,pos=?,crossType=?,crossPos=?,maxType=?,maxPos=?,getDailyRewardTime=?,totalRobTimes=?,"
                + "totalBeRobTimes=?,totalDailyRewardCount=?,worshipValue=?,modifyTime=?,lastBeAttackNotifyTime=?,estateSkinId=? where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getType());
            call.setInt(i++, data.getPos());
            call.setInt(i++, data.getCrossType());
            call.setInt(i++, data.getCrossPos());
            call.setInt(i++, data.getMaxType());
            call.setInt(i++, data.getMaxPos());
            call.setLong(i++, data.getGetDailyRewardTime());
            call.setLong(i++, data.getTotalRobTimes());
            call.setLong(i++, data.getTotalBeRobTimes());
            call.setString(i++, data.getTotalDailyRewardCount().toString());
            call.setInt(i++, data.getWorshipValue());
            call.setLong(i++, data.getModifyTime());
            call.setLong(i++, data.getLastBeAttackNotifyTime());
            call.setInt(i++, data.getEstateSkinId());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserEstateWarData parseData(ResultSet rs) throws SQLException {
        UserEstateWarData data = new UserEstateWarData();
        data.setUserId(rs.getLong("userId"));
        data.setType(rs.getInt("type"));
        data.setPos(rs.getInt("pos"));
        data.setCrossType(rs.getInt("crossType"));
        data.setCrossPos(rs.getInt("crossPos"));
        data.setWorshipValue(rs.getInt("worshipValue"));
        data.setModifyTime(rs.getInt("modifyTime"));
        data.setMaxType(rs.getInt("maxType"));
        data.setMaxPos(rs.getInt("maxPos"));
        data.setGetDailyRewardTime(rs.getLong("getDailyRewardTime"));
        data.setTotalRobTimes(rs.getLong("totalRobTimes"));
        data.setTotalBeRobTimes(rs.getLong("totalBeRobTimes"));
        String value = rs.getString("totalDailyRewardCount");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setTotalDailyRewardCount(new BigInteger(value));
        }
        data.setLastBeAttackNotifyTime(rs.getLong("lastBeAttackNotifyTime"));
        data.setEstateSkinId(rs.getInt("estateSkinId"));
        return data;
    }
}
